package com.spider.admin.controller;

import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.service.client.ClientService;
import com.spider.admin.store.primary.model.ClientInfo;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @GetMapping("/list/{tenantId}")
    public PageResult<ClientInfo> queryClientListByTenantId(
            @PathVariable String tenantId,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String clientName,
            @RequestParam(required = false) String archiveStatus)
            throws SpiderException {
        return clientService.queryClientByTenantId(tenantId, page, pageSize, clientName, archiveStatus);
    }

    /**
     * 新增客户端
     *
     * @param clientInfo
     * @return
     * @throws SpiderException
     */
    @PostMapping("/add")
    public int addClient(@RequestBody @Validated(ClientInfo.Save.class) ClientInfo clientInfo) throws SpiderException {

        return clientService.saveClient(clientInfo);
    }

    /**
     * 更新客户端信息
     * @param clientId
     * @param clientInfo
     * @return
     * @throws SpiderException
     */
    @PutMapping("/update/{clientId}")
    public int updateClient(@PathVariable String clientId, @RequestBody @Validated(ClientInfo.Update.class) ClientInfo clientInfo)throws SpiderException{
        return  clientService.updateClient(clientInfo,clientId);
    }

    /**
     * 激活客户端
     *
     * @return
     */
    @PutMapping("/active/{clientId}")
    public int activeClient(@PathVariable("clientId") @NotBlank String clientId) throws SpiderException {
        return clientService.activeClient(clientId);
    }


    /**
     * 归档客户端
     *
     * @return
     */
    @PutMapping("/archive/{clientId}")
    public int archiveClient(@PathVariable("clientId") @NotBlank String clientId) throws SpiderException {
        return clientService.archiveClient(clientId);
    }

    /**
     * 更换客户端Key
     *
     * @param clientId
     * @return
     */
    @PutMapping("/changeKey/{clientId}")
    public String changeClientKey(@PathVariable("clientId") @NotBlank String clientId) throws SpiderException {
        return clientService.changeClientKey(clientId);
    }

    /**
     * 删除客户端
     * @param clientId
     * @return
     * @throws SpiderException
     */
    @DeleteMapping("/remove/{clientId}")
    public int removeClient(@PathVariable("clientId") @NotBlank String clientId) throws SpiderException {
        return clientService.removeClient(clientId);
    }
}

